package org.example.module_login.utlis;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Base64;

/**
 * 加密解密
 *
 * @author HAJ
 * @since 2025-03-03 15:14:34
 */
public class Encryption {

    /**
     * 私钥
     * */
    private static final String PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCEoLONxSCFobFt\n" +
            "o+/u9SI7VtE6qlCR5GTw8u0YWKNXSU+baEGMMGvjotOm0ZgwFzj1gIsiTUQNkNxK\n" +
            "JOl5WYV/ikc6H5chUdzyFYRdMZzEs/73MKmC3pLvBCORLbwDGpaB5b1R4B6Tgi08\n" +
            "I0RcgD5pmRpDxLKk5T0l49TgzDhRkoX9FI2CK+11Emr6WOTGCcxcneExwRWfQeOl\n" +
            "0HrvZCjgLix2xbdxtgLh/gl5QHMOfuFA257EGJACZEiaOqYWRlRM+0RnA5XRHEo3\n" +
            "ATNTXJ556YvVybRvRyA8b+/jLd6qNRmpykKUVo/52W/IQYmGPDW+EVMvDQNTSmf5\n" +
            "6uuBMzWbAgMBAAECggEACUBadgOvqpKmWUMLvAvuaNw7+Q8jUqHvZp1Ta5TXRAHx\n" +
            "oyIJHHnONNFJWUQajRQ5nWpriieAnT+jFirv/E6B0O043bWRaboXou1cT9HjoRb7\n" +
            "LIwA7weOkUylHH3ntQG4SPRZGep7x4dXl2/QV3F8qQoMeniwi28QLmHvmgS00v+/\n" +
            "UpWlMPWt2rH3FUWolLp7syvuyc47AU1E7On7mSOLQtPE0r2D0Yjr/exVs3hdtJ60\n" +
            "p3R8NlOLtP5jj8IQNj9ioYcXKmecAuKRTe0QX+TlowPIHi3YHt/wNw/hWzV8of0I\n" +
            "MlizYbNKvAveCvDoYhmyEXAOlS3cvyjh9cWjKuU+zQKBgQC6bbwC5jE2oqY3ve9v\n" +
            "GX+T2Nn7OradPD9E/PSv0kSkwqfFJScwSxNyAUmiAIbZgJbm7ejKru5JQ25T8KDJ\n" +
            "ztaqiG9x60dnfSiRrq88bA/qYZfWT++oZQlNT96aqhs5+7Hb59vpM7I3/xqWmp+g\n" +
            "GHJNw+2L3Ubg2WjwPXWuzbcf9wKBgQC2HydGVynnYVewWk1M4owoIFTThydSBopS\n" +
            "J4/NSnl0zolc9V6oWwloJaY6vTx1vmDdVeF1mD6hUoNZtGbf/E7Tk3NW+j/scf27\n" +
            "67naoFVhsn1E2tUhXQhseUKaSvksLEcIJSrQvsRLeQSP8S8mxoPQVogEELTAR8hC\n" +
            "65qQXKm2fQKBgQCP1gJtKjVmNe+QqlaFnWK1K2ZjU1V50vXtwEz5Kn4TsKvIi3XE\n" +
            "6OfD2gJ5n3Zrca771Sr61iS7Ge+Ri+JCUfeF82YouqKI0iPu4pwJPka5Szre+yWp\n" +
            "Aus+XxUct4nyHqOHS0h8kIo8cfKNqBboanQmU7h/ZOY+rP8t+/1ENSvI2QKBgAth\n" +
            "MPQNODoT9AW57sCSG03aj1JUTJmsfPIbNLoQIDyzNxQO5OZCIflW+OcMsf1tcuIB\n" +
            "nr/ZTniIDlsJkbAHNOm1eeyCYIImsUMPvKIziII9Q+SMg+q8xGz6urWnX6Bp99pD\n" +
            "cERw1xUcL7PKzzZuCwh2j7MpVBH7qkC8NgE1OQmFAoGBAJ4OMt3MYJXBwUemipdK\n" +
            "1hxNpaX7TS7i3522cbPPTJ+8SyBY1KAmWQ5HYU9+s5tgPsz4VYGib7BI5VVWCzRk\n" +
            "VElg9SBvq6cd2PLMSZf21GrgSuXd/X8iquxt/4SikfETcLDUDI0TaxtdtPlApQ/R\n" +
            "tVDwW7y9tFdVTcBCCbRiRgRI";

    /**
     * 解密
     * */
    public static String decryptPassword(String encryptedPassword) {
        // 创建 RSA 实例，并指定使用私钥
        RSA rsa = new RSA(PRIVATE_KEY,null); // privateKeyString 是你的私钥字符串
        // 使用 rsa 实例进行解密操作
        return rsa.decryptStr(encryptedPassword, KeyType.PrivateKey);
    }

    /**
     * 生成盐值
     * */
    public static String generateSalt() {
        byte[] salt = new byte[16];
        new SecureRandom().nextBytes(salt);
        return Base64.getEncoder().encodeToString(salt);
    }

    /**
     * 加盐哈希
     * */
    public static String hashPassword(String password, String salt) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(Base64.getDecoder().decode(salt));
            byte[] hashedPassword = digest.digest(password.getBytes());
            return Base64.getEncoder().encodeToString(hashedPassword);
        } catch (Exception e) {
            throw new RuntimeException("Hashing failed", e);
        }
    }

    // 用户注册
    public static String registerUser(String password,String salt) {
        return hashPassword(decryptPassword(password), salt);
    }

    // 用户登录
    public static boolean loginUser(String frontPassword,String afterPassword,String salt) {
        String hashedPassword = hashPassword(decryptPassword(frontPassword), salt);
        return hashedPassword.equals(afterPassword);
    }

}
